package com.xinzhitong.www.controller;

import com.github.pagehelper.PageHelper;
import com.xinzhitong.www.enumeration.PostOrderType;
import com.xinzhitong.www.exception.consign.*;
import com.xinzhitong.www.exception.consigncondition.NoConsignEnrollException;
import com.xinzhitong.www.exception.consigngeneralexception.NoConsignGeneralException;
import com.xinzhitong.www.exception.post.NullPostSavedException;
import com.xinzhitong.www.service.ConsignService;
import com.xinzhitong.www.utils.Message;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 委培控制器
 *
 * @author 魏骆淳
 */
@CrossOrigin
@RestController
@RequestMapping("consign")
public class ConsignController {

    @Resource
    ConsignService consignService;

    /**
     * 获取委培信息
     *
     * @param consignId 委培id
     * @return 委培信息
     */
    @RequestMapping(value = "getConsignInformation", method = RequestMethod.GET)
    public Message consignInformation(
            @RequestParam("consignId") Integer consignId
    ) {
        try {
            return Message.createSuc(consignService.getConsignInformation(consignId));
        } catch (NullConsignInformationException e) {
            e.printStackTrace();
            return Message.createErr("委培信息为空");
        }
    }

    /**
     * 获取委培概要
     *
     * @param consignId 委培id
     * @return 委培概要
     */
    @RequestMapping(value = "getConsignOutline", method = RequestMethod.GET)
    public Message consignOutline(
            @RequestParam("consignId") Integer consignId
    ) {
        try {
            return Message.createSuc(consignService.getConsignInformation(consignId));
        } catch (NullConsignInformationException e) {
            e.printStackTrace();
            return Message.createErr("委培概要为空");
        }
    }

    /**
     * 委培报名
     *
     * @param consignId 委培id
     * @return 委培报名是否成功信息
     */
    @RequestMapping(value = "applyConsign", method = RequestMethod.POST)
    public Message consignApply(
            @RequestParam("consignId") Integer consignId
    ) {
        String msg = "报名成功";
        try {
            consignService.applyConsign(consignId);
            return Message.createSuc(msg);
        } catch (FullConsignApply e) {
            e.printStackTrace();
            msg = "报名人数已满";
        } catch (ConsignApplyRepetitionException e) {
            e.printStackTrace();
            msg = "重复报名";
        } catch (NullConsignIdException e) {
            e.printStackTrace();
            msg = "委培为空";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取委培报名信息
     *
     * @param consignId 委培id
     * @return 委培报名信息
     */
    @RequestMapping(value = "getConsignApplyInformation", method = RequestMethod.GET)
    public Message consignApplyInformation(
            @RequestParam("consignId") Integer consignId
    ) {
        try {
            return Message.createSuc(consignService.getConsignApplyInformation(consignId));
        } catch (NullConsignApplyInformationException e) {
            e.printStackTrace();
            return Message.createErr("委培报名信息为空");
        }
    }

    /**
     * 获取委培时间和地点
     *
     * @param consignId 委培id
     * @return 委培时间和地点
     */
    @RequestMapping(value = "getConsignTimeAndLocation", method = RequestMethod.GET)
    public Message consignLocation(
            @RequestParam("consignId") Integer consignId
    ) {
        try {
            return Message.createSuc(consignService.getConsignTimeAndLocation(consignId));
        } catch (NullConsignLocationAndTimeException e) {
            e.printStackTrace();
            return Message.createErr("委培地点和时间为空");
        }
    }

    /**
     * 根据筛选条件获取委培
     *
     * @param conditions 条件合集
     *                   city 市
     *                   professionId 工种id
     *                   positionId 职位id
     *                   sexRequest 性别要求
     *                   minAgeRequest 最小年龄要求
     *                   maxAgeRequest 最大年龄要求
     *                   eduRequest 学历要求
     *                   minExpRequest 最小经验要求
     *                   maxExpRequest 最大经验要求
     * @return 符合筛选条件的委培
     */
    @ResponseBody
    @RequestMapping(value = "consignSatisfyTheConditionsOf", method = RequestMethod.POST)
    public Message consignSatisfyTheConditionsOf(
            @RequestBody(required = false) Map<String, Object> conditions,
            @RequestParam(value = "pageNo") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
            @RequestParam(value = "orderType", required = false, defaultValue = "NEW") PostOrderType consignOrderType
    ) {
        String msg;
        PageHelper.startPage(pageNo, pageSize);
        if (conditions == null) {
            conditions = new HashMap<>();
        }
        try {
            return Message.createSuc(consignService.getConsignSatisfyTheConditionOf(
                    conditions,
                    consignOrderType,
                    pageNo,
                    pageSize
            ));
        } catch (NullConsignException e) {
            e.printStackTrace();
            msg = "暂无符合要求的委培";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取某月委培日期
     *
     * @param month 月
     * @param year  年
     * @return 委培日期
     */
    @RequestMapping(value = "consignTime", method = RequestMethod.GET)
    public Message getConsignTime(
            @RequestParam("month") Integer month,
            @RequestParam("year") Integer year
    ) {
        try {
            return Message.createSuc(consignService.getConsignTime(year, month));
        } catch (NullConsignTimeException e) {
            e.printStackTrace();
            return Message.createErr("暂无委培");
        }
    }

    /**
     * 获取委培系统消息
     *
     * @param id 委培id
     * @return 委培系统消息
     */
    @RequestMapping(value = "consignSystemMessage", method = RequestMethod.GET)
    public Message getConsignSystemMessage(@RequestParam("id") Integer id) {
        try {
            return Message.createSuc(consignService.getConsignSystemMessage(id));
        } catch (NullConsignSystemMessage e) {
            e.printStackTrace();
            return Message.createErr("暂无委培系统消息");
        }
    }

    /**
     * <<<<<<< HEAD
     * 批量删除委培
     *
     * @param idList 委培id列表
     * @return 删除成功信息
     */
    @RequiresRoles(value = {"COMPANY"})
    @RequestMapping(value = "deleteConsign", method = RequestMethod.DELETE)
    public Message deleteConsign(
            @RequestBody List<Integer> idList
    ) {
        return Message.createSuc(consignService.deleteConsign(idList));
    }

    /**
     * 获取委培报名详情
     *
     * @param consignId 委培id
     * @return 委培报名详情
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "consignCondition", method = RequestMethod.GET)
    public Message postCondition(
            @RequestParam(value = "consignId") Integer consignId
    ) {
        try {
            return Message.createSuc(consignService.getConsignCondition(consignId));
        } catch (NoConsignEnrollException e) {
            e.printStackTrace();
            return Message.createErr("此委培暂无人报名");
        }
    }

    /**
     * 获取新报名人数
     *
     * @return 人数
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "consignNewEnroll", method = RequestMethod.GET)
    public Message consignNewEnroll(
    ) {
        return Message.createSuc(consignService.getConsignNewEnroll());
    }

    /**
     * 下架委培
     *
     * @param id 委培id
     * @return 下架是否成功
     */
    @RequiresRoles(value = {"COMPANY"})
    @RequestMapping(value = "updateConsign", method = RequestMethod.PUT)
    public Message updateConsign(
            @RequestParam(value = "id") Integer id
    ) {
        return Message.createSuc(consignService.underConsign(id));
    }

    /**
     * 获取委培概览 可以关键字模糊搜索
     *
     * @param word     关键字
     * @param state    状态
     * @param pageNo   页码
     * @param pageSize 页大小
     * @return 委培信息
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "consignGeneral", method = RequestMethod.GET)
    public Message postGeneral(
            @RequestParam(value = "word", required = false, defaultValue = "") String word,
            @RequestParam(value = "state", required = false, defaultValue = "") String state,
            @RequestParam("pageNo") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize
    ) {
        try {
            return Message.createSuc(consignService.getConsignGeneral(state, word, pageNo, pageSize));
        } catch (NoConsignGeneralException e) {
            e.printStackTrace();
            return Message.createErr("查询不到信息");
        }
    }

    /**
     * 保存新增委培
     *
     * @param map 委培
     */
    @RequiresRoles(value = {"COMPANY"})
    @RequestMapping(value = "consignSave", method = RequestMethod.POST)
    public Message saveConsign(@RequestBody Map<String, Object> map) {
        consignService.saveConsign(map);
        String msg = "保存成功";
        return Message.createSuc(msg);
    }

    /**
     * 获取保存的委培信息
     *
     * @return 委培信息
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "consignSaved", method = RequestMethod.GET)
    public Message getSavedConsign() {
        try {
            return Message.createSuc(consignService.getSavedConsign());
        } catch (NullPostSavedException e) {
            return Message.createErr("未保存");
        }
    }

    /**
     * 发布新增委培
     *
     * @param positionId           职位id
     * @param requirePopulation    要求人数
     * @param contact              联系方式
     * @param address              地址
     * @param baseSalary           基础工资
     * @param maxSalary            最大工资
     * @param minSalary            最小工资
     * @param paySalaryDate        发薪日期
     * @param payType              结算方式
     * @param salaryOtherNotice    工资其他说明
     * @param liveNotice           住宿说明
     * @param foodNotice           饮食说明
     * @param socialSecurityNotice 社保说明
     * @param liveFoodOtherNotice  其他说明
     * @param jobText              工作内容
     * @param jobTimeNotice        工作时间说明
     * @param jobWay               工作方式
     * @param jobOtherNotice       工作其他说明
     * @param sexRequest           性别要求
     * @param maxAgeRequest        最大年龄要求
     * @param minAgeRequest        最小年龄要求
     * @param eduBackground        学历要求
     * @param minExpRequest        最小经验要求
     * @param maxExpRequest        最大经验要求
     * @param credentialRequest    证书要求
     * @param employOtherNotice    雇佣其他说明
     * @param startTime            开始时间
     * @param endTime              结束时间
     * @param deadline             截止时间
     * @param applyRequest         报名要求
     * @param otherNotice          其他说明
     */
    @RequiresRoles(value = {"COMPANY"})
    @RequestMapping(value = "consignRelease", method = RequestMethod.PUT)
    public Message releaseConsign(
            @RequestParam("positionId") Integer positionId,
            @RequestParam("requirePopulation") Integer requirePopulation,
            @RequestParam("contact") String contact,
            @RequestParam("address") String address,
            @RequestParam("baseSalary") double baseSalary,
            @RequestParam("minSalary") double minSalary,
            @RequestParam("maxSalary") double maxSalary,
            @RequestParam("paySalaryDate") Integer paySalaryDate,
            @RequestParam("payType") String payType,
            @RequestParam("salaryOtherNotice") String salaryOtherNotice,
            @RequestParam("liveNotice") String liveNotice,
            @RequestParam("foodNotice") String foodNotice,
            @RequestParam("socialSecurityNotice") String socialSecurityNotice,
            @RequestParam("liveFoodOtherNotice") String liveFoodOtherNotice,
            @RequestParam("jobText") String jobText,
            @RequestParam("jobTimeNotice") String jobTimeNotice,
            @RequestParam("jobWay") String jobWay,
            @RequestParam("jobOtherNotice") String jobOtherNotice,
            @RequestParam("sexRequest") Boolean sexRequest,
            @RequestParam("maxAgeRequest") Integer maxAgeRequest,
            @RequestParam("minAgeRequest") Integer minAgeRequest,
            @RequestParam("eduBackground") String eduBackground,
            @RequestParam("minExpRequest") Integer minExpRequest,
            @RequestParam("maxExpRequest") Integer maxExpRequest,
            @RequestParam("credentialRequest") String credentialRequest,
            @RequestParam("employOtherNotice") String employOtherNotice,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime,
            @RequestParam("deadline") String deadline,
            @RequestParam("applyRequest") String applyRequest,
            @RequestParam("otherNotice") String otherNotice
    ) {
        consignService.releaseConsign(
                positionId,
                requirePopulation,
                contact,
                address,
                baseSalary,
                maxSalary,
                minSalary,
                paySalaryDate,
                payType,
                salaryOtherNotice,
                liveNotice,
                foodNotice,
                socialSecurityNotice,
                liveFoodOtherNotice,
                jobText,
                jobTimeNotice,
                jobWay,
                jobOtherNotice,
                sexRequest,
                maxAgeRequest,
                minAgeRequest,
                eduBackground,
                minExpRequest,
                maxExpRequest,
                credentialRequest,
                employOtherNotice,
                startTime,
                endTime,
                deadline,
                applyRequest,
                otherNotice
        );
        String msg = "提交成功,待审核";
        return Message.createSuc(msg);
    }

    /**
     * 获取委培完整信息
     *
     * @param consignId 委培id
     * @return 委培完整信息
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "consignTotalInformation", method = RequestMethod.GET)
    public Message getConsignTotalInformation(@RequestParam("consignId") Integer consignId) {
        String msg;
        try {
            return Message.createSuc(consignService.getConsignTotalInformation(consignId));
        } catch (NullConsignIdException e) {
            e.printStackTrace();
            msg = "岗位不存在";
        }
        return Message.createErr(msg);
    }

    /**
     * 编辑委培
     *
     * @param consignId            委培id
     * @param positionId           职位id
     * @param requirePopulation    要求人数
     * @param contact              联系方式
     * @param address              地址
     * @param baseSalary           基础工资
     * @param maxSalary            最大工资
     * @param minSalary            最小工资
     * @param paySalaryDate        发薪日期
     * @param payType              结算方式
     * @param salaryOtherNotice    工资其他说明
     * @param liveNotice           住宿说明
     * @param foodNotice           饮食说明
     * @param socialSecurityNotice 社保说明
     * @param liveFoodOtherNotice  其他说明
     * @param jobText              工作内容
     * @param jobTimeNotice        工作时间说明
     * @param jobWay               工作方式
     * @param jobOtherNotice       工作其他说明
     * @param sexRequest           性别要求
     * @param maxAgeRequest        最大年龄要求
     * @param minAgeRequest        最小年龄要求
     * @param eduBackground        学历要求
     * @param minExpRequest        最小经验要求
     * @param maxExpRequest        最大经验要求
     * @param credentialRequest    证书要求
     * @param employOtherNotice    雇佣其他说明
     * @param startTime            开始时间
     * @param endTime              结束时间
     * @param deadline             截止时间
     * @param applyRequest         报名要求
     * @param otherNotice          其他说明
     */
    @RequiresRoles(value = {"COMPANY"})
    @RequestMapping(value = "consignEdit", method = RequestMethod.PUT)
    public Message editConsign(
            @RequestParam("consignId") Integer consignId,
            @RequestParam("positionId") Integer positionId,
            @RequestParam("requirePopulation") Integer requirePopulation,
            @RequestParam("contact") String contact,
            @RequestParam("address") String address,
            @RequestParam("baseSalary") double baseSalary,
            @RequestParam("minSalary") double minSalary,
            @RequestParam("maxSalary") double maxSalary,
            @RequestParam("paySalaryDate") Integer paySalaryDate,
            @RequestParam("payType") String payType,
            @RequestParam("salaryOtherNotice") String salaryOtherNotice,
            @RequestParam("liveNotice") String liveNotice,
            @RequestParam("foodNotice") String foodNotice,
            @RequestParam("socialSecurityNotice") String socialSecurityNotice,
            @RequestParam("liveFoodOtherNotice") String liveFoodOtherNotice,
            @RequestParam("jobText") String jobText,
            @RequestParam("jobTimeNotice") String jobTimeNotice,
            @RequestParam("jobWay") String jobWay,
            @RequestParam("jobOtherNotice") String jobOtherNotice,
            @RequestParam("sexRequest") Boolean sexRequest,
            @RequestParam("maxAgeRequest") Integer maxAgeRequest,
            @RequestParam("minAgeRequest") Integer minAgeRequest,
            @RequestParam("eduBackground") String eduBackground,
            @RequestParam("minExpRequest") Integer minExpRequest,
            @RequestParam("maxExpRequest") Integer maxExpRequest,
            @RequestParam("credentialRequest") String credentialRequest,
            @RequestParam("employOtherNotice") String employOtherNotice,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime,
            @RequestParam("deadline") String deadline,
            @RequestParam("applyRequest") String applyRequest,
            @RequestParam("otherNotice") String otherNotice
    ) {
        consignService.editConsign(
                consignId,
                positionId,
                requirePopulation,
                contact,
                address,
                baseSalary,
                maxSalary,
                minSalary,
                paySalaryDate,
                payType,
                salaryOtherNotice,
                liveNotice,
                foodNotice,
                socialSecurityNotice,
                liveFoodOtherNotice,
                jobText,
                jobTimeNotice,
                jobWay,
                jobOtherNotice,
                sexRequest,
                maxAgeRequest,
                minAgeRequest,
                eduBackground,
                minExpRequest,
                maxExpRequest,
                credentialRequest,
                employOtherNotice,
                startTime,
                endTime,
                deadline,
                applyRequest,
                otherNotice
        );
        String msg = "修改成功,待审核";
        return Message.createSuc(msg);
    }

    /**
     * 获取委培基本信息
     *
     * @param consignId 委培id
     * @return 委培基本信息
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "consignBasicInformation", method = RequestMethod.GET)
    public Message getConsignBasicInformation(@RequestParam("consignId") Integer consignId) {
        String msg;
        try {
            return Message.createSuc(consignService.getConsignBasicInformation(consignId));
        } catch (NullConsignIdException e) {
            e.printStackTrace();
            msg = "委培ID为空";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取委培重要指标
     *
     * @param consignId 委培id
     * @return 委培重要指标
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "consignIndex", method = RequestMethod.GET)
    public Message getConsignIndex(@RequestParam("consignId") Integer consignId) {
        String msg;
        try {
            return Message.createSuc(consignService.getConsignIndex(consignId));
        } catch (NullConsignIdException e) {
            e.printStackTrace();
            msg = "委培ID为空";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取委培基本信息和重要指标
     *
     * @param consignId 委培id
     * @return 委培基本信息和重要指标
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "consignInformationAndIndex", method = RequestMethod.GET)
    public Message getConsignInformationAndIndex(@RequestParam("consignId") Integer consignId) {
        String msg;
        try {
            Map<String, Object> consignInformation = consignService.getConsignBasicInformation(consignId);
            Map<String, Object> consignIndex = consignService.getConsignIndex(consignId);
            consignIndex.put("consignName", consignInformation.get("consignName"));
            consignIndex.put("companyName", consignInformation.get("companyName"));
            consignIndex.put("contact", consignInformation.get("contact"));
            return Message.createSuc(consignIndex);
        } catch (NullConsignIdException e) {
            e.printStackTrace();
            msg = "委培ID为空";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取最近编辑委培
     *
     * @return 最近编辑委培
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "recentEditorConsign", method = RequestMethod.GET)
    public Message getRecentEditorConsign() {
        try {
            return Message.createSuc(consignService.getRecentEditorConsign());
        } catch (NullRecentEditorConsign e) {
            return Message.createErr("无最近编辑委培");
        }
    }

    /**
     * 获取委培报名率
     *
     * @param consignId 委培id
     * @return 委培报名率
     */
    @RequiresRoles(value = {"ADMIN"})
    @RequestMapping(value = "consignEnrollment", method = RequestMethod.GET)
    public Message getConsignEnrollment(@RequestParam Integer consignId) {
        try {
            Map<String, Object> consignIndex = consignService.getConsignIndex(consignId);
            float enrollment = (float) ((long) consignIndex.get("enrollCount")) / (long) consignIndex.get("clickCount");
            return Message.createSuc(String.format("%.2f", enrollment));
        } catch (NullRecentEditorConsign e) {
            return Message.createErr("无最近编辑委培");
        }
    }

    /**
     * 获取委培概览
     *
     * @param positionId 工种id
     * @param pageNo     页码
     * @param pageSize   页大小
     * @param keyWord    关键词
     * @return 委培概览
     */
    @RequiresRoles(value = {"ADMIN"})
    @RequestMapping(value = "consignOutline", method = RequestMethod.GET)
    public Message getConsignOutline(
            @RequestParam Integer positionId,
            @RequestParam Integer pageNo,
            @RequestParam Integer pageSize,
            @RequestParam String keyWord
    ) {
        return Message.createSuc(consignService.getConsignOutline(positionId, pageNo, pageSize, keyWord));
    }

    /**
     * 机构端获取对应委培概览
     *
     *  @param pageNo     页码
     *  @param pageSize   页大小
     *  @param state      审核状态
     *  @return 对应机构委培概览
     * @author 吴展鹏
     **/
    @ResponseBody
    @RequestMapping(value = "consignOrganOutline",method = RequestMethod.GET)
    public Message getConsignOrganOutline(
            @RequestParam Integer pageNo,
            @RequestParam Integer pageSize,
            @RequestParam String state
    ){
        return Message.createSuc(consignService.getConsignOrganOutline(pageNo,pageSize,state));
    }

    /*
     * 机构确认委培信息,修改对应的post状态
     * @param consignId 委培ID
     * @return 机构确认委培信息
     * @author 吴展鹏
    * */
    @ResponseBody
    @RequestMapping(value = "consignOrganConfirm",method = RequestMethod.POST)
    public Message updateConsignOrganConfirm(
            @RequestParam Integer consignId
    ){
        consignService.updateConsignOrganConfirm(consignId);
        String msg = "修改成功";
        return Message.createSuc(msg);
    }


    /*
    *
    * 管理端确认接受委培的机构，给consign表的对应consignId加organId
    * @Param consignId
    * @param organId
    * @return 管理端确认委培机构
    * @author 吴展鹏
    * */
    @ResponseBody
    @RequestMapping(value = "consignInsertOrganId",method = RequestMethod.POST)
    public Message updateConsignInsertOrganId(
            @RequestParam Integer consignId,
            @RequestParam Integer organId
    ){
        String msg = consignService.updateConsignInsertOrganId(consignId,organId);
        return Message.createSuc(msg);
    }
}
